<?php

namespace Library\Prizes;

use Library\CardCode;
/**
 *
 *
 * @author pufan
 */
abstract class Prizes {
    const HONGBAO_RAND = '1';
    const HONGBAO_FIXED = '2';  //card_type 暂时还没有配置，全部为随机红包
    const AD = '3';  //页面广告
    const LINK = '4';  //链接类广告
    const Entity = '5'; //实物类

    protected $desc;  //奖品描述数组
    protected $ad_desc;  //广告描述数组
    protected $winning_time;
    protected $sn_number;  //流水号
    protected $cardCode;
    protected $valid;

    protected $prizes_order_model;
    protected $card_model;
    protected $cards_list_model;
    protected $user_prizes_count_model; //用户中奖数量表

    public function __construct($desc) {
        $this->valid = TRUE;
        $this->prizes_order_model = D('prizes_order');
        $this->card_model = D('card');
        $this->cards_list_model = D('cards_list');
        $this->user_prizes_count_model = D('user_prizes_count');
        $this->cardCode = CardCode::getInstance();

        /*
         * 图片地址添加oss 域名
         */
        if (isset($desc['center_pic'])) {
            if ($desc['center_pic'] != '') {
                $desc['center_pic'] =   C('IMG_PATH').$desc['center_pic'];
            }
        }
        $this->desc = $desc;
    }

    function is_valid() {
        return $this->valid;
    }

    protected function init_order() {
        $this->sn_number = $this->get_snno();
        $this->winning_time = date('Y-m-d H:i:s', time());
        $this->save_order();
    }

    public function get_type() {
        return $this->desc['type'];
    }

    public function get_desc() {
        return $this->desc;
    }

    protected function get_order_attr($attr_name) {
        return $this->desc[$attr_name];
    }

    protected function set_order_attr($attr_name, $value) {
        return $this->desc[$attr_name] = $value;
    }

    abstract protected function save_order();

    /*
     * 统计中奖次数
     */
    protected function update_user_prizes_count($card_id, $openid) {

        $card['id'] = $card_id;
        $result = $this->card_model->where($card)->setInc('card_get_count_day', 1);

        $user_prizes['card_id'] = $card_id;
        $user_prizes['openid'] = $openid;
        $user_prizes_num = $this->user_prizes_count_model->lock(true)->where($user_prizes)->find();
        \Think\Log::record($user_prizes_num);

        $user_prizes_count = $user_prizes;
        if (is_array($user_prizes_num)) {
            $user_prizes_count['cur_day_num'] = $user_prizes_num['cur_day_num'] + 1;
            $user_prizes_count['total_num'] = $user_prizes_num['total_num'] + 1;
            $user_prizes_count['update_time'] = $this->winning_time;

            $result = $this->user_prizes_count_model->where($user_prizes)->save($user_prizes_count);
        } else {
            $user_prizes_count['cur_day_num'] = 1;
            $user_prizes_count['total_num'] = 1;
            $user_prizes_count['update_time'] = $this->winning_time;

            $result = $this->user_prizes_count_model->where($user_prizes)->add($user_prizes_count);
        }
        return $result;
    }

    protected function create_analytics_url() {
        $mark_id = $this->get_order_attr('card_id');
        $mark_type = $this->get_type();
        $mark = $mark_id+ 1000000;
        $url = U('Portal/Index/analytics', array('mark'=> $mark,'ctype'=> $mark_type,'real_url'=>$this->get_order_attr('center_url')));
        if ($this->get_order_attr('center_url') == '') {
            return '';
        }
        return $url;
    }
    public function get_Analytics() {
        $analytics['money'] = $this->get_order_attr('money') != '' ? $this->get_order_attr('money') : 0;
        $analytics['cardnum'] = $this->get_order_attr('card_num') != '' ? $this->get_order_attr('card_num') : 0;
        $analytics['sn'] = $this->sn_number != '' ? $this->sn_number : 0;
        return $analytics;
    }

    /* 生成流水号
     * yyyymmdd+10
     */
    protected function get_snno() {
        return date("Ymd").mt_rand(1000000000, 9999999999);
    }
}